package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Override
    public R<ShoppingCart> addToCart(ShoppingCart cart) {
        //获取用户信息
        Long userId = BaseContext.getCurrentId();
        cart.setUserId(userId);
        //判断当前用户添加的是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        if (cart.getDishId() != null) {
            wrapper.eq(ShoppingCart::getDishId, cart.getDishId());

        }
        if (cart.getSetmealId() != null) {
            wrapper.eq(ShoppingCart::getSetmealId, cart.getSetmealId());
        }
        //判断之前是否有数据，有的话就数量加1，没有就插入一条数据

        ShoppingCart shoppingCart = this.getOne(wrapper);
       /* if (!ObjectUtils.isEmpty(shoppingCart)) {

            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            cart = shoppingCart;


            //this.updateById(cart);
        } else {
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            //this.save(cart);
        }*/

        if (ObjectUtils.isEmpty(shoppingCart)) {
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCart = cart;
        } else {
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
        }
        this.saveOrUpdate(shoppingCart);
        return R.success(shoppingCart);
    }

    @Override
    public R<List<ShoppingCart>> shoppingCartList() {
        //获取当前用户Id
        Long userId = BaseContext.getCurrentId();
        if (ObjectUtils.isEmpty(userId)) {
            return R.error("登录过期，重新登陆");
        }
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = this.list(wrapper);
        return R.success(list);
    }

    @Override
    public R<String> deleteShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        if (ObjectUtils.isEmpty(userId)) {
            return R.error("登录过期，重新登陆");
        }
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        this.remove(wrapper);
        return R.success("清空成功！");
    }

    @Override
    public R<ShoppingCart> sub(Long dishId, Long setmealId) {
        if (ObjectUtils.isEmpty(dishId) && ObjectUtils.isEmpty(setmealId)) {
            return R.error("参数错误！");
        }

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(dishId), ShoppingCart::getDishId, dishId);
        wrapper.eq(!ObjectUtils.isEmpty(setmealId), ShoppingCart::getSetmealId, setmealId);
        ShoppingCart one = this.getOne(wrapper);
        Integer number = one.getNumber();
        one.setNumber(number - 1);
        this.updateById(one);
        if (number == 1) {
            this.removeById(one);
        }
        return R.success(one);
    }
}